{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "class TextCNN(nn.Module):\n",
    "    def __init__(self, **args):\n",
    "        super(TextCNN, self).__init__()\n",
    "        self.args = args\n",
    "        \n",
    "        V = args['embed_num']\n",
    "        D = args['embed_dim']\n",
    "        embedding = torch.from_numpy(args['embed_mat'])\n",
    "        C = args['class_num']\n",
    "        Ci = 1\n",
    "        Co = args['kernel_num']\n",
    "        Ks = args['kernel_sizes']\n",
    "        dropout = args['dropout']\n",
    "        \n",
    "        self.embed = nn.Embedding(V, D)\n",
    "        self.embed.weight.data.copy_(embedding)\n",
    "        self.convs = nn.ModuleList([nn.Conv2d(Ci, Co, (K, D)) for K in Ks])\n",
    "        self.dropout = nn.Dropout(dropout)\n",
    "        self.fc1 = nn.Linear(len(Ks)*Co, C)\n",
    "        \n",
    "    def forward(self, x):\n",
    "        x = self.embed(x)\n",
    "        if self.args['static']:\n",
    "            x = x.detach()\n",
    "        x = x.unsqueeze(1)\n",
    "        x = [F.relu(conv(x)).squeeze(3) for conv in self.convs]\n",
    "        x = [F.max_pool1d(i, i.size(2)).squeeze(2) for i in x]\n",
    "        x = torch.cat(x, 1)\n",
    "        x = self.dropout(x)\n",
    "        logit = F.sigmoid(self.fc1(x))\n",
    "        return logit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "word_embed = np.load('../../data_preprocess/embed/word_embed_mat.npy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "textCNN = TextCNN(embed_num=411722, embed_dim=256, embed_mat=word_embed, class_num=1999, kernel_num=100, kernel_sizes=[1,2,3,4,5], dropout=0.5, static=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([64, 1999])\n"
     ]
    }
   ],
   "source": [
    "from torch.autograd import Variable\n",
    "input = torch.from_numpy(np.ones((64, 71)).astype('int32')).long()\n",
    "input = Variable(input)\n",
    "output = textCNN(input)\n",
    "print output.size()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
